package com.fjh.shop.orders.service.interceptors;

import com.fjh.shop.common.Result;
import com.fjh.shop.orders.service.handlers.BaseContextHandler;
import com.fjh.shop.user.feign.api.UserClient;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 授权拦截器
 *
 * @author wuyunbin
 * @since 2025-07-02
 */
@Slf4j
@Component
public class AuthorizationInterceptor implements HandlerInterceptor {
    @Lazy
    @Resource
    private UserClient userClient;

    /**
     * 拦截器前置处理
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();

        Result<Long> result = userClient.getMemberIdByToken();
        if(result.getCode() != 20000) {
            log.info("获取用户ID失败，错误信息：{}", result.getMsg());
            return false;
        }

        BaseContextHandler.set( result.getData());

        return true;
    }
}
